<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>用户类别设置</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<%@include file="common.jsp"%>
</head>
<body>
	<div style="margin-top:20px;">
		<button onclick="addUserTypeBox()" id="userTypeBox" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#addUserTypeBox">新增用户类别</button>
	</div>
	<div>
		<table id="userTypeTable" class="text-nowrap"></table>
	</div>
	<!-- 新增银行产品信息窗口 -->
	<div class="modal fade" id="addUserTypeBox" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">添加用户类别</h4>
                <h4 class="modal-title" id="myModalLabel2">修改用户类别</h4>
            </div>
            <div class="modal-body">
           		 <form id="addUserTypeFrom" method='post' class="" enctype="multipart/form-data">
				 	<div class="form-group">
				    	<label for="exampleInputName2">类别编码：</label>
						<input id="userTypeCode" name="userTypeCode" type="number" class="form-control" placeholder="请输入编码"/>
						<input id="userTypeId" name="id" hidden="ture"/>
				  	</div>
				  	<div class="form-group">
				    	<label for="exampleInputEmail2">类别名称：</label>
				    	<input id="userTypeName" name="userTypeName" class="form-control" placeholder="请输入名称"/>
				  	</div>
				  	<div class="form-group">
				  		<label for="exampleInputEmail2">用户奖励：</label>
				  		<input id="userAward" name="userAward" type="number" class="form-control" placeholder="用户奖励"/>
				  	</div>
				</form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button id="addUserTypeBtn" type="button" class="btn btn-default" onclick="addUserType()">提交</button>
                <button id="updateUserTypeBtn" type="button" class="btn btn-default" onclick="updateUserType()">修改</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
	</div>
</body>
<script type="text/javascript">
$(function(){
	//加载表格
	$("#userTypeTable").bootstrapTable({
		url: "${pageContext.request.contextPath}/user/findUserType.do",
		method: "post",
		cache: false,
		dataType: "json",
		contentType: "application/x-www-form-urlencoded",
		toolbar: "#toolbar",
		classes: "table table-hover table-striped",
		height:450,
		pagination: true,
		sidePagination: 'server',
		search: false,
		smartDisplay:false,
		sortOrder: "desc",
		searchText: "",
		showRefresh: false,
		showToggle: false,
		singleSelect: true,
		showPaginationSwitch: false,
		pageNumber: 1,
		pageSize: 5,
		pageList: [5, 10, 20, 40],
		clickToSelect: true,
		onCheck: function (row) {
		},
		queryParamsType: "undefined",
		queryParams: function (params) {
			var params = {
				pageNum: params.pageNumber,
				pageSize: params.pageSize,
				sortBy: params.sortOrder,
			}
			return params;
		},
		columns: [
			{
				field:	"id",
				title:	"id",
				visible: false,
			},	{
				field: "userTypeCode",
				title: "类别编码"
			}, {
				field: "userTypeName",
				title: "类别名称"
			}, {
				field: "userAward",
				title: "用户奖励"
			}, {
				field: "createTime",
				title: "创建时间",
				formatter: formatTime
			}
			
		],
		responseHandler: function (data) {//onLoad前处理数据源
			return data;
		},
		onLoadSuccess: function (data) {	//成功时执行
		},
		onLoadError: function (data) {	//失败时执行
			return;
		},
		//双击行打开图片
		onDblClickRow:function(row){
			//打开修改窗口
			$('#userTypeBox').trigger("click");
			//窗口标题显示/隐藏
			$('#myModalLabel').css("display","none");
			$('#myModalLabel2').css("display","block");
			//按钮显示/隐藏
			$('#addUserTypeBtn').hide();
			$("#updateUserTypeBtn").show();
			
			//控制赋值
			$('#userTypeCode').val(row.userTypeCode);
			$('#userTypeName').val(row.userTypeName);
			$('#userAward').val(row.userAward);
			//控制置灰
			$('#userTypeCode').attr("disabled","disabled");
			$('#userTypeName').attr("disabled","disabled");
			//ID隐藏域赋值
			$('#userTypeId').val(row.id);
		}
	});
})
//打开新增窗口
function addUserTypeBox(){
	//窗口标题显示/隐藏
	$('#myModalLabel').css("display","block");
	$('#myModalLabel2').css("display","none");
	//按钮显示/隐藏
	$('#addUserTypeBtn').show();
	$("#updateUserTypeBtn").hide();
	//清空表单
	document.getElementById("addUserTypeFrom").reset()
	//取消置灰
	$('#userTypeCode').removeAttr("disabled");
	$('#userTypeName').removeAttr("disabled");
}
//新增用户类别
function addUserType(){
	if($('#userTypeCode').val() == ''){
		alert("请输入用户类别编码！");
		return;
	}
	if($('#userTypeName').val() == ''){
		alert("请输入用户类别名称！");
		return;
	}
	if($('#userAward').val() == ''){
		alert("请输入用户奖励！");
		return;
	}
	if(confirm("确定提交?")){
		//ajax方式提交表单
		/* var data = $('#addUserTypeFrom').serialize();
		var submitData = decodeURIComponent(data,true);
		$.ajax({
			url:"${pageContext.request.contextPath}/user/addUserType.do",
			contentType:"application/x-www-form-urlencoded",
			data:submitData,
			type:"post",
			cache:false,//false是不缓存，true为缓存
		    async:true,//true为异步，false为同步
			success:function(data){
				alert(data);
			}
		}) */
		//只传递编码做为查询条件
		var datas = "userTypeCode="+$('#userTypeCode').val();
		$.ajax({
		url:"${pageContext.request.contextPath}/user/findUserType.do",
		contentType:"application/x-www-form-urlencoded",
		//只把编码传递到后台
		data: datas,
		type:"post",
		processData: false,
		cache:false,//false是不缓存，true为缓存
	    async:true,//true为异步，false为同步
		success:function(data){
			var datas = jQuery.parseJSON(data);
			if(datas.rows.length > 0){
				return alert('编码存在！');
			}else{
				//提交表单，新增信息
				$('#addUserTypeFrom').attr("action","${pageContext.request.contextPath}/user/addUserType.do");
				$('#addUserTypeFrom').submit();
				alert('新增成功！');
			}
		}
		})
	}
}
//修改用户类别
function updateUserType(){
	if($('#userAward').val() == ''){
		alert("请输入用户奖励！");
		return;
	}
	if(confirm("确定提交?")){
		//提交表单，修改信息
		$('#addUserTypeFrom').attr("action","${pageContext.request.contextPath}/user/updateUserType.do");
		$('#addUserTypeFrom').submit();
		alert('修改成功！');
	}
}
</script>
</html>




